package com.qf.shopping.controll;

import com.qf.shopping.dto.systemDTO.UserDTO;
import com.qf.shopping.dto.systemDTO.UserProfileDTO;
import com.qf.shopping.pojo.System.User;
import com.qf.shopping.service.AdminMangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admins")
public class AdminMangerControll {

    @Autowired
    private AdminMangerService adminMangerService=null;

    //查询所有管理员
    @GetMapping("/admins")
    public String getList(Model model){
        List<UserDTO> list=adminMangerService.getUserByAll();
        model.addAttribute("admins",list);
        return  "/admins/list";
    }

    //新建
    @GetMapping("/addadmin")
    public String toaddUser(UserProfileDTO user){

        return "/admins/add";
    }

    @PostMapping("/addadmin")
    public String addUser(UserProfileDTO user){
        adminMangerService.addAdmin(user);
        return "redirect:/admins/admins";
    }

    //修改
    @GetMapping("/uppdateadmin/{id}")
    public String toupdateUser(@PathVariable("id") Integer id,Model model) {
            UserProfileDTO user=adminMangerService.findUserById(id);
            model.addAttribute("admin",user);
            return "/admins/update";
    }

    @PutMapping("/uppdateadmin")
    public String toupdateUser(UserProfileDTO u){
        adminMangerService.updateAdmin(u);
        return "redirect:/admins/admins";
    }

    //删除
    @GetMapping("/deleteadmin/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        adminMangerService.deleteAdmin(id);
        return  "redirect:/admins/admins";
    }


}
